package com.example.cat_eye.method_channel

import android.annotation.SuppressLint
import android.content.Context
import android.widget.Toast
import io.flutter.plugin.common.BinaryMessenger
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import java.text.SimpleDateFormat

/**
 *
 * @Package: com.example.cat_eye.method_channel
 * @ClassName: MethodChannel
 * @Author: szj
 * @CreateDate: 8/6/21 11:16 AM
 */
@SuppressLint("StaticFieldLeak")
object MethodChannelPlugin : MethodChannel.MethodCallHandler {
    //通道名称
    private const val METHOD_CHANNEL_NAME = "method_channel"


    private lateinit var context: Context

    /**
     * 注册
     */
    fun register(messenger: BinaryMessenger, context: Context) {
        this.context = context
        val channel = MethodChannel(messenger, METHOD_CHANNEL_NAME)
        channel.setMethodCallHandler(this)
    }


    override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) {
        when (call.method) {
            "toast" -> {
                //参考文档:[https://blog.csdn.net/weixin_44819566/article/details/110235518]
                Toast.makeText(
                    context, "flutter 当前时间为:${SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(
                        System.currentTimeMillis()
                    )}", Toast.LENGTH_SHORT
                ).show()
                result.success("我是Android端的数据")
            }
            else -> {
                // 表明没有对应实现
                result.notImplemented()
            }
        }
    }


}